﻿using System;
using System.Configuration;
using System.Web.Configuration;
using System.Collections.ObjectModel;

namespace VioletDevelopmentLaboratory.Net.Web.Configuration
{
	public sealed class HealthMonitoringSectionManager : ConfigurationManagerBase
	{
		private HealthMonitoringSection section;

		public HealthMonitoringSectionManager()
		{
			this.section = (HealthMonitoringSection)base.GetConfigurationSection("system.web/healthMonitoring");
		}

		public void Modify(bool enabled, TimeSpan heartbeatInterval)
		{
			this.section.Enabled = enabled;
			this.section.HeartbeatInterval = heartbeatInterval;
			this.Save();
		}
		public HealthMonitoringSection GetHealthMonitoringSection()
		{
			return this.section;
		}





		public void AddBufferMode(
			string name, int maxBufferSize, int maxFlushSize, int urgentFlushThreshold,
			TimeSpan regularFlushInterval, TimeSpan urgentFlushInterval, int maxBufferThreads)
		{
			this.section.BufferModes.Add(
				new BufferModeSettings(name, maxBufferSize, maxFlushSize, urgentFlushThreshold,
					regularFlushInterval, urgentFlushInterval, maxBufferThreads));

			this.Save();
		}
		public void ModifyBufferMode(
			string name, int maxBufferSize, int maxFlushSize, int urgentFlushThreshold,
			TimeSpan regularFlushInterval, TimeSpan urgentFlushInterval, int maxBufferThreads)
		{
			this.section.BufferModes[name].MaxBufferSize = maxBufferSize;
			this.section.BufferModes[name].MaxFlushSize = maxFlushSize;
			this.section.BufferModes[name].UrgentFlushThreshold = urgentFlushThreshold;
			this.section.BufferModes[name].RegularFlushInterval = regularFlushInterval;
			this.section.BufferModes[name].UrgentFlushInterval = urgentFlushInterval;
			this.section.BufferModes[name].MaxBufferThreads = maxBufferThreads;
			this.Save();
		}
		public void RemoveBufferMode(string name)
		{
			this.section.BufferModes.Remove(name);
			this.Save();
		}
		public void ClearBufferModes()
		{
			this.section.BufferModes.Clear();
			this.Save();
		}
		public BufferModeSettings GetBufferModeSettings(string name)
		{
			return this.section.BufferModes[name];
		}
		public BufferModesCollection GetBufferModeSettingsCollection()
		{
			return this.section.BufferModes;
		}






		public void AddEventMapping(string name, string type, int startEventCode, int endEventCode)
		{
			this.section.EventMappings.Add(
				new EventMappingSettings(name, type, startEventCode, endEventCode));
			this.Save();
		}
		public void ModifyEventMapping(string name, string type, int startEventCode, int endEventCode)
		{
			this.section.EventMappings[name].Type = type;
			this.section.EventMappings[name].StartEventCode = startEventCode;
			this.section.EventMappings[name].EndEventCode = endEventCode;
			this.Save();
		}
		public void RemoveEventMapping(string name)
		{
			this.section.EventMappings.Remove(name);
			this.Save();
		}
		public void ClearEventMappings()
		{
			this.section.EventMappings.Clear();
			this.Save();
		}
		public EventMappingSettings GetEventMappingSettings(string name)
		{
			return this.section.EventMappings[name];
		}
		public EventMappingSettingsCollection GetEventMappingSettingsCollection()
		{
			return this.section.EventMappings;
		}





		public void AddProfile(string name, int minInstances, int maxLimit, TimeSpan minInterval)
		{
			this.section.Profiles.Add(
				new ProfileSettings(name, minInstances, maxLimit, minInterval));
			this.Save();
		}
		public void ModifyProfile(string name, int minInstances, int maxLimit, TimeSpan minInterval)
		{
			this.section.Profiles[name].MinInstances = minInstances;
			this.section.Profiles[name].MaxLimit = maxLimit;
			this.section.Profiles[name].MinInterval = minInterval;
			this.Save();
		}
		public void RemoveProfile(string name)
		{
			this.section.Profiles.Remove(name);
			this.Save();
		}
		public void ClearProfile()
		{
			this.section.Profiles.Clear();
			this.Save();
		}
		public ProfileSettings GetProfileSettings(string name)
		{
			return this.section.Profiles[name];
		}
		public ProfileSettingsCollection GetProfileSettingsCollection()
		{
			return this.section.Profiles;
		}







		public void AddProvider(HealthMonitoringProviderSettings provider)
		{
			HealthMonitoringProviderSettings.Add(this.section.Providers, provider);

			this.Save();
		}
		public void ModifyProvider(HealthMonitoringProviderSettings provider)
		{
			HealthMonitoringProviderSettings.Modify(this.section.Providers, provider);

			this.Save();
		}
		public void RemoveProvider(HealthMonitoringProviderSettings provider)
		{
			HealthMonitoringProviderSettings.Remove(this.section.Providers, provider);

			this.Save();
		}
		public void ClearProvider()
		{
			this.section.Providers.Clear();
		}
		public HealthMonitoringProviderSettings GetProvider(string name)
		{
			return HealthMonitoringProviderSettings.ConvertToHealthMonitoringProviderSettings(
				this.section.Providers[name]
			);
		}
		public Collection<HealthMonitoringProviderSettings> GetProviders()
		{
			Collection<HealthMonitoringProviderSettings> providers = new Collection<HealthMonitoringProviderSettings>();

			foreach (ProviderSettings provider in this.section.Providers)
			{
				providers.Add(
					HealthMonitoringProviderSettings.ConvertToHealthMonitoringProviderSettings(provider)
				);
			}

			return providers;
		}









		public void AddRule(
			string name, string eventName, string provider, string profile,
			int minInstances, int maxLimit, TimeSpan minInterval, string custom)
		{
			this.section.Rules.Add(
				new RuleSettings(name, eventName, provider, profile, minInstances, maxLimit, minInterval, custom));
			this.Save();
		}
		public void ModifyRule(
			string name, string eventName, string provider, string profile,
			int minInstances, int maxLimit, TimeSpan minInterval, string custom)
		{
			this.section.Rules[name].EventName = eventName;
			this.section.Rules[name].Provider = provider;
			this.section.Rules[name].Profile = profile;
			this.section.Rules[name].MinInstances = minInstances;
			this.section.Rules[name].MaxLimit = maxLimit;
			this.section.Rules[name].MinInterval = minInterval;
			this.section.Rules[name].Custom = custom;

			this.Save();

		}
		public void RemoveRule(string name)
		{
			this.section.Rules.Remove(name);
			this.Save();
		}
		public void ClearRules()
		{
			this.section.Rules.Clear();
			this.Save();
		}
		public RuleSettings GetRuleSettings(string name)
		{
			return this.section.Rules[name];
		}
		public RuleSettingsCollection GetRuleSettingsCollection()
		{
			return this.section.Rules;
		}
	}
}
